import React, { Component } from 'react'
import { Container, Content } from 'native-base'
import { View, TouchableOpacity, Text, DeviceEventEmitter, Clipboard } from 'react-native'
import LinearGradient from 'react-native-linear-gradient'
import SplashScreen from 'react-native-splash-screen'
import User from '../../components/user/index'
import Nav from './components/nav'
import Tips from './components/tips'
import Forbiduser from '../../components/forbiduser'
import commonStyle from '../../libs/commonStyle'
import DialogCom from '../../components/dialog'
import TipsCom from '../../components/dialog/tips'
import { navList, addAtrrHandle, getAwardOneApi } from './data'
import { connect } from 'react-redux' // 引入connect函数
import * as loginAction from '../../redux/actions/login'

class Home extends Component {
	constructor(props) {
		super(props)
		this.state = {
			dialogVisible: false,
			dialogContent: '',
			dialogTitle: '',
			
			tipsVisible: false,
			tipsContent: ''
		}
	}
	onEnter(){
		this.props.getHomeData && this.props.getHomeData()
	}
	componentWillMount() {
		if (SplashScreen && SplashScreen.hide) {
			SplashScreen.hide() // 关闭启动屏幕
		}
		DeviceEventEmitter.addListener('onenter.home', () => {
			this.onEnter()
		})
		DeviceEventEmitter.addListener('forbid.user', () => {
			global.Pop.show(<Forbiduser wechat={this.props.serviceInfo.wechat} />,{animationType: 'slide-up'})
		})
	}
	
	// 获取剪贴板内容
	async getClipboard () {
		let content = await Clipboard.getString()
		return content
	}
	
	async componentDidMount () {
		DeviceEventEmitter.addListener('login.success', () => {
			this.getAward()
		})
		this.getAward()
		this.getClipboard()
	}
	async getAward () {
		let res = await getAwardOneApi()
		if (res) {
			this.setState({
				dialogVisible: true,
				dialogContent: `恭喜你获得每日登录奖励${res}元`,
				dialogTitle: '每日登录奖励'
			})
		}
	}
	showDialog(text) {
		this.setState({
			dialogVisible: true,
			dialogContent: text
		})
	}
	showTips(text) {
		this.setState({
			tipsVisible: true,
			tipsContent: text
		})
		setTimeout(() => {
			this.setState({
				tipsVisible: false
			})
		}, 1000)
	}
	render() {
		const { userinfo, serviceInfo, homeStat } = this.props
		const list = addAtrrHandle(navList, {
			...homeStat,
			parentId: userinfo && userinfo.parentId
		})
		return (
			<Container>
				<Content style={{ backgroundColor: '#f5f5f5' }}>
					<View
						style={[
							commonStyle.flexItem,
							commonStyle.backgroundColorGray
						]}
					>
						<LinearGradient
							colors={['#C30D23', '#C30D00', '#A60000']}
						>
							<User />
						</LinearGradient>
						<Nav list={list} showDialog={this.showTips.bind(this)} />
						<Tips info={serviceInfo} showDialog={this.showTips.bind(this)} />
					</View>
					<DialogCom 
						onClose={() => {
							this.setState({
								dialogVisible: false
							})
						}}
						titleStyle={
							{
								textStyle: {
									color: '#C30D23'
								}
							}
						}
						title={this.state.dialogTitle}
						content={this.state.dialogContent}
						visible={this.state.dialogVisible} />
						
					<TipsCom 
						onClose={() => {
							this.setState({
								tipsVisible: false
							})
						}}
						visible={this.state.tipsVisible} 
						content={this.state.tipsContent} />
				</Content>
			</Container>
		)
	}
}

export default connect(
	state => ({
		userinfo: state.user.user || {},
		homeStat: state.login.homeData,
		serviceInfo: state.login.serviceInfo
	}),
	dispatch => ({
		getHomeData: () => dispatch(loginAction.getHomeData())
	})
)(Home)
